package com.fare.taluo.core.domain.crawl.impl;

import java.io.Serializable;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.dayatang.hibernate.EntityRepositoryHibernate;
import com.fare.taluo.core.domain.crawl.FdcCrawlStatisticsForGrabDateEntity;
import com.fare.taluo.core.domain.crawl.FdcCrawlStatisticsForGrabDateEntityRepository;
import com.fare.taluo.util.SpringUtil;

@Repository("fdcCrawlStatisticsForGrabDateEntityRepository")
public class FdcCrawlStatisticsForGrabDateEntityRepositoryImpl extends EntityRepositoryHibernate implements FdcCrawlStatisticsForGrabDateEntityRepository{
	private JdbcTemplate jdbctemp=(JdbcTemplate) SpringUtil.getBean("jdbcTemplate");
	
	@SuppressWarnings("unchecked")
	@Override
	public List<FdcCrawlStatisticsForGrabDateEntity> findByGrabDate(Date date1, Date date2,Date grabDate) {
		List<FdcCrawlStatisticsForGrabDateEntity> list=new ArrayList<FdcCrawlStatisticsForGrabDateEntity>();
		
		
		/*
		//Hibernate查询
		StringBuffer jpql =new StringBuffer("from FdcCrawlStatisticsForGrabDateEntity plane where 1=1 ");
		List<Object> conditionVals = new ArrayList<Object>();
		if(date1 !=null && !date1.equals("")){
			jpql.append(" and plane.grabDate >=? ");
			conditionVals.add(date1);	
		}
		if( date2 !=null && !date2.equals("")){
			jpql.append(" and plane.grabDate <=? ");
			conditionVals.add(date2);
		}
		if(grabDate !=null && !grabDate.equals("")){
			jpql.append(" and plane.grabDate =? ");
			conditionVals.add(grabDate);	
		}
		list =super.find(jpql.toString(),conditionVals.toArray(),FdcCrawlStatisticsForGrabDateEntity.class);
		*/
		
		//jdbc查询
		StringBuffer jpql =new StringBuffer("select * from FDC_CRAWL_STATISTICS_GRABDATE where 1=1 ");
		if(date1 !=null && !date1.equals("")){
			jpql.append(" and grab_date >= '"+date1+"'");
		}
		if( date2 !=null && !date2.equals("")){
			jpql.append(" and grab_date <= '"+date2+"'");
		}
		if(grabDate !=null && !grabDate.equals("")){
			jpql.append(" and grab_date = '"+grabDate+"'");
		}
		list=(List<FdcCrawlStatisticsForGrabDateEntity>)jdbctemp.query(jpql.toString(), new ItemMapper());
		return list;
	}

	@SuppressWarnings({ "serial", "rawtypes" })
	protected class ItemMapper implements RowMapper ,Serializable  {

		@Override
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			FdcCrawlStatisticsForGrabDateEntity item=new FdcCrawlStatisticsForGrabDateEntity();
			item.setCreateTime(rs.getDate("create_time"));
			item.setDayTotalSourceData(rs.getLong("day_source_data"));
			item.setGrabDate(rs.getDate("grab_date"));
			item.setDayTotalLineNumbers(rs.getLong("day_line_numbers"));
			item.setLineByChannel(rs.getString("line_by_channel"));
			item.setSourceByChannel(rs.getString("source_by_channel"));
			item.setLineCityByChannel(rs.getString("line_city_by_channel"));
			item.setId(rs.getString("id"));
			
			return item;
		}
		
	}

}
